package ControllerLag;
import ModelLag.*;
import DBLag.*;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
/**
 * @author DM75 Gruppe 3
 * @version 2011-12-22
 */
public class OrderArrCTR {
    private IFDbOrderArr dbOrderArr;

    public OrderArrCTR() {
    dbOrderArr = new DbOrderArr();
    }

    /**
     * @param orderID
     * @return OrderArr
     */
    public OrderArr findOrderArr(int orderID) {
        return dbOrderArr.findOrderArr(orderID);
    }

    /**
     * @return the next orderID
     */
    public int getNextOrderID()
    {
        return dbOrderArr.getNextOrderID();
    }
    /**
     * @param searchString
     * @return arraylist of found orders
     */
    public ArrayList<OrderArr> searchOrderArr(String searchString) {
        return dbOrderArr.searchOrderArr(searchString);
   }
    
    /**
     * @param orderID
     * @param date
     * @param startTime
     * @param endTime
     * @param amountOfLanes
     * @param noOfPersons
     * @param noOfPersonsOnlyFood
     * @param customer
     * @param employee
     * @param room
     * @param event
     * @param description
     * @param bowlingFrom
     * @return confirmation code
     */
    public int createOrderArr(int orderID, String date, String startTime, String endTime, int amountOfLanes, int noOfPersons, int noOfPersonsOnlyFood, Customer customer, Employee employee, Room room, Event event,String description, String bowlingFrom)
    {
        double totalPrice = noOfPersons*event.getPricePrPerson() + noOfPersonsOnlyFood*event.getPricePrPersonOnlyFood();
        SimpleDateFormat onlyTimeOfDay = new SimpleDateFormat("HH:MM");
        SimpleDateFormat wholeDate = new SimpleDateFormat("dd-MM-yyyy");
        OrderArr newOrder = new OrderArr();
        newOrder.setOrderID(dbOrderArr.getNextOrderID());
        try{         
        GregorianCalendar newDate = new GregorianCalendar();
        newDate.setTime(wholeDate.parse(date));
        newOrder.setDate(newDate);
        newOrder.setStartTime(startTime);
        
        newOrder.setEndTime(endTime);
        
        newOrder.setAmountOfLanes(amountOfLanes);
        newOrder.setNoOfPersons(noOfPersons);
        newOrder.setNoOfPersonsOnlyFood(noOfPersonsOnlyFood);
        newOrder.setTotalPrice(totalPrice);
        newOrder.setCustomer(customer);
        newOrder.setEmployee(employee);
        newOrder.setRoom(room);
        newOrder.setEvent(event);
        newOrder.setDescription(description);      
        newOrder.setBowlingFrom(bowlingFrom);      
       
        return dbOrderArr.insertOrderArr(newOrder);
        }catch(Exception e){
            System.out.println("Error in createOrderArr in CTR err: " + e.getMessage());
            return -1;
        }  
    }

    /**
     * @return
     */
    public ArrayList<OrderArr> getAllOrderArr() {
        return dbOrderArr.getAllOrderArr();
    }
    /**
     * @param isPeriod get all orders, or orders from period
     * @param from period from
     * @param to period to
     * @return arraylist with all the orders
     */
    public ArrayList<OrderArr> getEverythingOrderArr(boolean isPeriod, String from, String to) {
        ArrayList list = null;
        try{
        if(!from.equals("") && !to.equals(""))
        {
        SimpleDateFormat sdf = new SimpleDateFormat("dd-mm-yyyy");// bruges måske til at sikre syntax i string
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-mm-dd");// bruges måske til at sikre syntax i string
        
        Date fromDate = sdf.parse(from);
        Date toDate = sdf.parse(to);
        list = dbOrderArr.getEverythingOrderArr(isPeriod,sdf2.format(fromDate), sdf2.format(toDate));
        }else
        {
            list = dbOrderArr.getEverythingOrderArr(isPeriod,"","");
        }
        return list;
        }catch(Exception e)
        {
            System.out.println("Error in periodsearch CTR: " + e.getMessage());
            return null;
        }
    
    }    


    /**
     * @param newOrder order to insert
     * @return confirmation code
     */
    public int insertOrderArr(OrderArr newOrder) {
        return dbOrderArr.insertOrderArr(newOrder); 
    }


    /**
     * @param orderID
     * @param date
     * @param startTime
     * @param endTime
     * @param amountOfLanes
     * @param noOfPersons
     * @param noOfPersonsOnlyFood
     * @param customer
     * @param employee
     * @param room
     * @param event
     * @param description
     * @param bowlingFrom
     * @return confirmation code
     */
    public int updateOrderArr(int orderID, GregorianCalendar date, String startTime, String endTime, int amountOfLanes, int noOfPersons, int noOfPersonsOnlyFood, Customer customer, Employee employee, Room room, Event event,String description,String bowlingFrom) {
        double totalPrice = noOfPersons*event.getPricePrPerson() + noOfPersonsOnlyFood*event.getPricePrPersonOnlyFood();
        OrderArr newOrder = new OrderArr(orderID, date, startTime, endTime, amountOfLanes, noOfPersons, noOfPersonsOnlyFood, totalPrice, customer, employee, room, event, description, bowlingFrom);
        return dbOrderArr.updateOrderArr(newOrder);
    }

    /**
     * @param orderID
     * @return confirmation code
     */
    public int deleteOrderArr(int orderID) {
        OrderArr newOrder = new OrderArr();
        newOrder.setOrderID(orderID);
        return dbOrderArr.deleteOrderArr(newOrder);
    }   
}
